<template>
	<router-view v-slot="{ Component }">
		<keep-alive>
			<component :is="Component" :key="refreshRouterViewKey" />
		</keep-alive>
	</router-view>
</template>

<script setup>
	import {
		useGlobal
	} from "@/stores/useGlobal";
	import pinia from "@/stores";
	import tool from "@/utils/tool";
	const global = useGlobal(pinia)
	import bus from "@/utils/bus";
	import { useRoute, useRouter } from "vue-router";
	const route = useRoute()
	const router = useRouter()
	import {
		computed,
		onBeforeMount,
		onMounted,
		ref
	} from "vue";

	const user = ref({})
	onBeforeMount(() => {
		tool.post('app/getUnit').then(ret => {
			global.units = ret.data
		})
		reload()
	})
	const refreshRouterViewKey = computed(() => {
		return route.fullPath
	})

	onMounted(() => {
		window.keyBack = keyBack // app返回键
		window.scanCode = scanCode // app返回扫码结果
		window.currentLocation = currentLocation // app 返回坐标
		// document.addEventListener("UniAPpJSBridgeReady", function () {
		//   uni.getEnv(function (res) {
		//   })
		// })
		// window.addEventListener("scanCode", function () {
		// })
		window.uniFunc = uniFunc // app 返回坐标
		// document.addEventListener("UniAPpJSBridgeReady", function () {
		//   uni.getEnv(function (res) {
		//   })
		// })
		// window.addEventListener("scanCode", function () {
		// })
		global.width = window.innerWidth
		global.height = window.innerHeight
		global.setIsMobile(true)
		setTimeout(() => {
			if (window.__wxjs_environment === 'miniprogram') {
				global.statusBarHeight = 0
			} else {
				getInfo()
			}
		}, 300)

		tool.post('app/getUpdateInfo').then(ret => {
			if (ret.stat) {
				setInterval(() => {
					updateVer()
				}, ret.data)
			}
		})
	})

	const updateVer = () => {
		let localVer = tool.data.get('ver') //
		tool.post('app/getVer').then(ret => {
			if (ret.stat) {
				let onlineVer = (ret.data && ret.data.ver) ? ret.data.ver : null
				if (localVer) {
					if (onlineVer) {
						if (localVer !== onlineVer) {
							// 本地版本与线上版本不一样，更新
							updateVers(ret)
						}
					}
				} else {
					if (onlineVer) {
						// 没有本地版本
						updateVers(ret)
					}
				}
			}
		})
	}
	const updateVers = (ret) => {
		let msg = ret.data.msg
		let onlineVer = (ret.data && ret.data.ver) ? ret.data.ver : null
		tool.confirm(msg, '更新提示' + (ret.data.needLogin ? '(需重新登录)' : '')).then(() => {
			tool.data.set('ver', onlineVer)
			tool.suc('更新中....')
			if (ret.data.needLogin) {
				tool.data.remove("v4TOKEN");
			}
			window.location.reload()
		})
	}
	const getInfo = () => {
		if (typeof uni !== 'undefined') {
			uni.postMessage({
				data: {
					type: 'func',
					pine: 'uni.getSystemInfoSync()'
				}
			})
		}
	}
	const reload = () => {
		let cacheDate = tool.data.get('cacheDate')
		if (cacheDate) {
			if (cacheDate !== tool.getDay()) {
				tool.suc('系统更新中。。。')
				window.location.reload()
				tool.data.set('cacheDate', tool.getDay())
			}
		} else {
			tool.suc('系统更新中。。。')
			window.location.reload()
			tool.data.set('cacheDate', tool.getDay())
		}
	}
	// uniapp返回键
	const keyBack = () => {
		if (route.name !== 'dashboard') { 
			router.back()
		} else {
			if (typeof uni !== 'undefined') {
				uni.postMessage({
					data: {
						type: 'back'
					}
				})
			}
		}
	}
	//
	const scanCode = (type, code) => {
		bus.$emit('scan-bar', code)
	}
	//
	const currentLocation = (long, lat) => {

	}
	const uniFunc = (ret) => {
		if (ret && ret.statusBarHeight) {
			global.statusBarHeight = ret.safeArea.top
		}
	}

</script>

<style lang="scss">
	//票据状态样式
	.billState {
		&.billState-draft {
			color: lightseagreen;
		}

		&.billState-del {
			color: red;
		}
	}

	// 禁用样式
	.cell-disabled {
		&:hover {
			cursor: pointer;
			color: #2db7f5;
		}

		&.disabled-1 {
			color: red
		}

		&.disabled-2 {
			color: red
		}
	}

	// 票据可点击字段样式
	.canClick {
		color: #2db7f5;

		&:hover {
			color: #ff9900;
			text-decoration: underline;
			cursor: pointer;
		}
	}

	//可弹出编辑字段样式
	.canPop {
		color: #2db7f5;

		&:hover {
			color: #ff9900;
			text-decoration: underline;
			cursor: pointer;
		}
	}

	// 表格行样式
	.rowClassName {}

	// 行操作按钮样式
	.rowBtn i {
		margin: 0 5px;
		color: #808695;
		font-size: 16px;

		&:hover {
			cursor: pointer;
			color: #2b85e4;
		}
	}

	.sum-cls {
		color: #F56C6C;
		font-size: 16px;
		font-weight: bold;
	}

	table {
		border-collapse: collapse !important;
	}

	/*文字不可选择*/
	.unselect {
		-webkit-user-select: none;
		-moz-user-select: none;
		-ms-user-select: none;
		user-select: none;
	}

	.padding20 {
		padding-left: 20px;
	}

	.padding40 {
		padding-left: 40px;
	}

	.padding44 {
		padding-left: 44px;
	}

	.padding60 {
		padding-left: 60px;
	}

	.padding64 {
		padding-left: 64px;
	}

	.vxe-grid--toolbar-wrapper {
		/*background-color: #808695;*/
		/*border-bottom: 1px solid #dcdee2;*/
	}

	.vxe-form {}

	.vxe-table {}

	.vxe-form--item-title {
		padding: 0 4px;
	}

	.vxe-form .vxe-form--item {
		padding: 2px !important;
	}

	.body--wrapper {
		/*background-color: #f8f8f9 !important;*/
	}

	.detail td.label {
		font-size: 16px;
		font-weight: bold;
	}

	.detail td.value {
		font-size: 16px;
	}

	.point {
		color: #2db7f5;

		&:hover {
			cursor: pointer;
			color: #2b85e4;
		}
	}
</style>